/**
 * 
 */
package kr.co.insoft.core.service.template.application.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.template.application.EventEntityModel;
import kr.co.insoft.core.repository.template.application.EventRepository;
import kr.co.insoft.core.service.template.application.EventService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Service
public class EventServiceImpl implements EventService {
	
	@Autowired
	EventRepository eventRepository;
	
	@Override
	public List<EventEntityModel> getList(Map<String, Object> param)
			throws ServiceException {
		try {
			return eventRepository.getList(param);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void createEvent(EventEntityModel eventEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			eventEntityModel.setAuth((User) auths.getPrincipal());
			/* Event 등록 */
			eventEntityModel = eventRepository.createEvent(eventEntityModel);
		} catch(Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public EventEntityModel getEvent(String eventid)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();

			EventEntityModel eventEntityModel = new EventEntityModel();
			eventEntityModel.setEventid(eventid);
			eventEntityModel.setAuth((User) auths.getPrincipal());
			return eventRepository.getEvent(eventEntityModel);

		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void doEditEvent(EventEntityModel eventEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			eventEntityModel.setAuth((User) auths.getPrincipal());

			eventRepository.doEditEvent(eventEntityModel);

		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void doRemoveEvent(String eventid)
			throws ServiceException {
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("eventid", eventid);
			eventRepository.doRemoveEvent(param);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public boolean isExistEventName(EventEntityModel eventEntityModel) 
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			eventEntityModel.setAuth((User) auths.getPrincipal());
			return eventRepository.isExistEventName(eventEntityModel) > 0;
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
}
